package com.glsc.ngateway.opmanage.service.performance;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.performance.DayPerformance;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.performance.DayPerformanceItem;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.performance.DayPerformanceItemRepository;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.performance.DayPerformanceRepository;
import com.glsc.ngateway.opmanage.dto.*;
import com.glsc.ngateway.opmanage.service.DesktopService;
import com.glsc.ngateway.opmanage.service.ledger.LedgerService;
import com.glsc.ngateway.opmanage.service.other.TradingDayService;
import com.glsc.ngateway.opmanage.utils.DateUtils;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static com.glsc.ngateway.opmanage.utils.DateUtils.formatDate;

@Service
@Slf4j
public class DayPerformanceService {


    @Resource
    private DayPerformanceRepository dayPerformanceRepository;

    @Resource
    private DayPerformanceItemRepository dayPerformanceItemRepository;

    @Resource
    private DesktopService desktopService;

    @Resource
    private LedgerService ledgerService;

    @Resource
    private TradingDayService tradingDayService;


    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<DayPerformance> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<DayPerformance> page = dayPerformanceRepository.findAll(SpecificationUtil.buildSpecification(param, DayPerformance.class), pageable);

        return page;
    }

    /**
     * 条件查找
     *
     * @param param
     * @return
     */
    public List<DayPerformance> listByCondition(Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        List<DayPerformance> page = dayPerformanceRepository.findAll(SpecificationUtil.buildSpecification(param, DayPerformance.class));

        return page;
    }


    /**
     * 根据用户姓名和日期查询
     *
     * @param userName
     * @param date
     * @return
     */
    public List<DayPerformance> findByUserNameAndDate(String userName, String date) {
        if (StringUtils.isBlank(date) || StringUtils.isBlank(userName)) {
            return null;
        }
        List<DayPerformance> list = dayPerformanceRepository.findAll(Example.of(DayPerformance.builder().userName(userName).effectDate(date).build()));
        return list;
    }

    /**
     * 根据岗位名称和日期查询
     *
     * @param postName
     * @param date
     * @return
     */
    public List<DayPerformance> findByPostNameAndDate(String postName, String date) {
        if (StringUtils.isBlank(date) || StringUtils.isBlank(postName)) {
            return null;
        }
        List<DayPerformance> list = dayPerformanceRepository.findAll(Example.of(DayPerformance.builder().postName(postName).effectDate(date).build()));
        return list;
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public DayPerformance findById(Integer id) {
        if (Objects.isNull(id)) {
            return null;
        }
        Optional<DayPerformance> optional = dayPerformanceRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public DayPerformance save(DayPerformance obj) {
        if (Objects.isNull(obj)) {
            return null;
        }
        return dayPerformanceRepository.save(obj);
    }

    /**
     * 查找某个用户这一时间段内的总的有效工时
     *
     * @param
     * @return
     */
    public Integer findUserSumManHourByPostName(String userOaAccount, String postName, LocalDateTime startTime, LocalDateTime endTime) {
        return dayPerformanceRepository.findUserSumManHour(userOaAccount, postName, startTime, endTime);
    }

    public Integer findUserSumManHour(String userOaAccount, LocalDateTime startTime, LocalDateTime endTime) {
        return dayPerformanceRepository.findUserSumManHourNoPostName(userOaAccount, startTime, endTime);
    }


    /**
     * 查找某个用户这一时间段内的总的主动工时
     *
     * @param userOaAccount
     * @param startTime
     * @param endTime
     * @return
     */
    public Integer findUserSumAccordHour(String userOaAccount, LocalDateTime startTime, LocalDateTime endTime) {
        return dayPerformanceRepository.findUserSumAccordHour(userOaAccount, startTime, endTime);
    }


    /**
     * 创建当日衍生品绩效数据
     */
    @XxlJob("performance_ysp")
    public void createYspPerformance() {

        //查询今天是否涉及交易日
        Boolean isTradeDay = tradingDayService.getToday(DateUtils.getCurrentDate(formatDate));
        if (!isTradeDay) {
            return;
        }
        String nowDateString = DateUtils.getNowDateString();
        ysp(nowDateString);
    }

    @XxlJob("one_ysp")
    public void test(){
        LocalDate start = LocalDate.of(2024, 8, 1);
        LocalDate end = LocalDate.of(2024, 11, 28);
        List<LocalDate> days = getDaysInRange(start, end);
        for (LocalDate day : days) {
            //查询今天是否涉及交易日
            Boolean isTradeDay = tradingDayService.getToday(day.toString().replaceAll("-",""));
            if (!isTradeDay) {
                continue;
            }
            ysp(day.toString());
        }
    }

    public static List<LocalDate> getDaysInRange(LocalDate start, LocalDate end) {
        List<LocalDate> days = new ArrayList<>();
        while (!start.isAfter(end)) {
            days.add(start);
            start = start.plusDays(1);
        }
        return days;
    }


    @Transactional
    public void ysp(String nowDateString) {

        PlatformResponse<LedgerDesktopDto> response = desktopService.screenYsp(nowDateString, null);
        if (Objects.isNull(response)) {
            return;
        }
        LedgerDesktopDto data = response.getData();
        if (Objects.isNull(data)) {
            return;
        }

        List<DayPerformance> list = Lists.newArrayList();
        List<DayPerformanceItem> itemList = Lists.newArrayList();

//        perfMap.put("孙伟平", LedgerPerfDtoItem.builder().userName("孙伟平").postName("股衍运营岗").userOaAccount("sunwp").swapHour(0).customerHour(0).foundationHour(0).incomeHour(0).manHour(0).settlementHour(0).manHour(0).build());
//        perfMap.put("陶轶铭", LedgerPerfDtoItem.builder().userName("陶轶铭").postName("股衍运营岗").userOaAccount("taoym").swapHour(0).customerHour(0).foundationHour(0).incomeHour(0).manHour(0).settlementHour(0).manHour(0).build());
//        perfMap.put("赵正彦", LedgerPerfDtoItem.builder().userName("赵正彦").postName("股衍运营岗").userOaAccount("zzy").swapHour(0).customerHour(0).foundationHour(0).incomeHour(0).manHour(0).settlementHour(0).manHour(0).build());
//        perfMap.put("毕翠翠", LedgerPerfDtoItem.builder().userName("毕翠翠").postName("股衍运营岗").userOaAccount("bicc").swapHour(0).customerHour(0).foundationHour(0).incomeHour(0).manHour(0).settlementHour(0).manHour(0).build());

        DayPerformance sunwpPerformance = DayPerformance.builder().userName("孙伟平").userOaAccount("sunwp").postName("股衍运营岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance taoymPerformance = DayPerformance.builder().userName("陶轶铭").userOaAccount("taoym").postName("股衍运营岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance zzyPerformance = DayPerformance.builder().userName("赵正彦").userOaAccount("zzy").postName("股衍运营岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance biccPerformance = DayPerformance.builder().userName("毕翠翠").userOaAccount("bicc").postName("股衍运营岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        list.add(sunwpPerformance);
        list.add(taoymPerformance);
        list.add(zzyPerformance);
        list.add(biccPerformance);

        List<LedgerPerfDtoItem> perfList = data.getPerfList();
        if (CollectionUtils.isNotEmpty(perfList)) {
            Map<String, LedgerPerfDtoItem> itemMap = perfList.stream().collect(Collectors.toMap(LedgerPerfDtoItem::getUserOaAccount, e -> e, (i, j) -> j));

            LedgerPerfDtoItem sunwpPerfDtoItem = itemMap.get("sunwp");
            if (Objects.nonNull(sunwpPerfDtoItem)) {
                sunwpPerformance.setManHour(sunwpPerfDtoItem.getManHour());
//                sunwpDto.setIncomeHour(dayPerformanceItemService.findUserSumManHour("sunwp", startTime, endTime, "shouyi_pingzheng"));
//                sunwpDto.setCustomerHour(dayPerformanceItemService.findUserSumManHour("sunwp", startTime, endTime, "kyc_org"));
//                sunwpDto.setSwapHour(dayPerformanceItemService.findUserSumManHour("sunwp", startTime, endTime, "jiaoyi_yewu"));
//                sunwpDto.setFoundationHour(dayPerformanceItemService.findUserSumManHour("sunwp", startTime, endTime, "jijin_shenshu"));
//                sunwpDto.setSettlementHour(dayPerformanceItemService.findUserSumManHour("sunwp", startTime, endTime, "qing_suan"));

                if (Objects.nonNull(sunwpPerfDtoItem.getIncomeHour()) && sunwpPerfDtoItem.getIncomeHour() > 0) {
                    DayPerformanceItem incomeHour = DayPerformanceItem.builder().userName("孙伟平").effectDate(nowDateString).userOaAccount("sunwp").ledgerType("shouyi_pingzheng").postName("股衍运营岗").manHour(sunwpPerfDtoItem.getIncomeHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(incomeHour);
                }

                if (Objects.nonNull(sunwpPerfDtoItem.getCustomerHour()) && sunwpPerfDtoItem.getCustomerHour() > 0) {
                    DayPerformanceItem customerHour = DayPerformanceItem.builder().userName("孙伟平").effectDate(nowDateString).userOaAccount("sunwp").ledgerType("kyc_org").postName("股衍运营岗").manHour(sunwpPerfDtoItem.getCustomerHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(customerHour);
                }

                if (Objects.nonNull(sunwpPerfDtoItem.getSwapHour()) && sunwpPerfDtoItem.getSwapHour() > 0) {
                    DayPerformanceItem swapHour = DayPerformanceItem.builder().userName("孙伟平").effectDate(nowDateString).userOaAccount("sunwp").ledgerType("jiaoyi_yewu").postName("股衍运营岗").manHour(sunwpPerfDtoItem.getSwapHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(swapHour);
                }

                if (Objects.nonNull(sunwpPerfDtoItem.getFoundationHour()) && sunwpPerfDtoItem.getFoundationHour() > 0) {
                    DayPerformanceItem foundationHour = DayPerformanceItem.builder().userName("孙伟平").effectDate(nowDateString).userOaAccount("sunwp").ledgerType("jijin_shenshu").postName("股衍运营岗").manHour(sunwpPerfDtoItem.getFoundationHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(foundationHour);
                }

                if (Objects.nonNull(sunwpPerfDtoItem.getSettlementHour()) && sunwpPerfDtoItem.getSettlementHour() > 0) {
                    DayPerformanceItem settlementHour = DayPerformanceItem.builder().userName("孙伟平").effectDate(nowDateString).userOaAccount("sunwp").ledgerType("qing_suan").postName("股衍运营岗").manHour(sunwpPerfDtoItem.getSettlementHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(settlementHour);
                }
            }

            LedgerPerfDtoItem taoymPerfDtoItem = itemMap.get("taoym");
            if (Objects.nonNull(taoymPerfDtoItem)) {
                taoymPerformance.setManHour(taoymPerfDtoItem.getManHour());

                if (Objects.nonNull(taoymPerfDtoItem.getIncomeHour()) && taoymPerfDtoItem.getIncomeHour() > 0) {
                    DayPerformanceItem incomeHour = DayPerformanceItem.builder().userName("陶轶铭").effectDate(nowDateString).userOaAccount("taoym").ledgerType("shouyi_pingzheng").postName("股衍运营岗").manHour(taoymPerfDtoItem.getIncomeHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(incomeHour);
                }

                if (Objects.nonNull(taoymPerfDtoItem.getCustomerHour()) && taoymPerfDtoItem.getCustomerHour() > 0) {
                    DayPerformanceItem customerHour = DayPerformanceItem.builder().userName("陶轶铭").effectDate(nowDateString).userOaAccount("taoym").ledgerType("kyc_org").postName("股衍运营岗").manHour(taoymPerfDtoItem.getCustomerHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(customerHour);
                }

                if (Objects.nonNull(taoymPerfDtoItem.getSwapHour()) && taoymPerfDtoItem.getSwapHour() > 0) {
                    DayPerformanceItem swapHour = DayPerformanceItem.builder().userName("陶轶铭").effectDate(nowDateString).userOaAccount("taoym").ledgerType("jiaoyi_yewu").postName("股衍运营岗").manHour(taoymPerfDtoItem.getSwapHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(swapHour);
                }

                if (Objects.nonNull(taoymPerfDtoItem.getFoundationHour()) && taoymPerfDtoItem.getFoundationHour() > 0) {
                    DayPerformanceItem foundationHour = DayPerformanceItem.builder().userName("陶轶铭").effectDate(nowDateString).userOaAccount("taoym").ledgerType("jijin_shenshu").postName("股衍运营岗").manHour(taoymPerfDtoItem.getFoundationHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(foundationHour);
                }

                if (Objects.nonNull(taoymPerfDtoItem.getSettlementHour()) && taoymPerfDtoItem.getSettlementHour() > 0) {
                    DayPerformanceItem settlementHour = DayPerformanceItem.builder().userName("陶轶铭").effectDate(nowDateString).userOaAccount("taoym").ledgerType("qing_suan").postName("股衍运营岗").manHour(taoymPerfDtoItem.getSettlementHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(settlementHour);
                }
            }

            LedgerPerfDtoItem zzyPerfDtoItem = itemMap.get("zzy");
            if (Objects.nonNull(zzyPerfDtoItem)) {
                zzyPerformance.setManHour(zzyPerfDtoItem.getManHour());

                if (Objects.nonNull(zzyPerfDtoItem.getIncomeHour()) && zzyPerfDtoItem.getIncomeHour() > 0) {
                    DayPerformanceItem incomeHour = DayPerformanceItem.builder().userName("赵正彦").effectDate(nowDateString).userOaAccount("zzy").ledgerType("shouyi_pingzheng").postName("股衍运营岗").manHour(zzyPerfDtoItem.getIncomeHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(incomeHour);
                }

                if (Objects.nonNull(zzyPerfDtoItem.getCustomerHour()) && zzyPerfDtoItem.getCustomerHour() > 0) {
                    DayPerformanceItem customerHour = DayPerformanceItem.builder().userName("赵正彦").effectDate(nowDateString).userOaAccount("zzy").ledgerType("kyc_org").postName("股衍运营岗").manHour(zzyPerfDtoItem.getCustomerHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(customerHour);
                }

                if (Objects.nonNull(zzyPerfDtoItem.getSwapHour()) && zzyPerfDtoItem.getSwapHour() > 0) {
                    DayPerformanceItem swapHour = DayPerformanceItem.builder().userName("赵正彦").effectDate(nowDateString).userOaAccount("zzy").ledgerType("jiaoyi_yewu").postName("股衍运营岗").manHour(zzyPerfDtoItem.getSwapHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(swapHour);
                }

                if (Objects.nonNull(zzyPerfDtoItem.getFoundationHour()) && zzyPerfDtoItem.getFoundationHour() > 0) {
                    DayPerformanceItem foundationHour = DayPerformanceItem.builder().userName("赵正彦").effectDate(nowDateString).userOaAccount("zzy").ledgerType("jijin_shenshu").postName("股衍运营岗").manHour(zzyPerfDtoItem.getFoundationHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(foundationHour);
                }

                if (Objects.nonNull(zzyPerfDtoItem.getSettlementHour()) && zzyPerfDtoItem.getSettlementHour() > 0) {
                    DayPerformanceItem settlementHour = DayPerformanceItem.builder().userName("赵正彦").effectDate(nowDateString).userOaAccount("zzy").ledgerType("qing_suan").postName("股衍运营岗").manHour(zzyPerfDtoItem.getSettlementHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(settlementHour);
                }
            }

            LedgerPerfDtoItem biccPerfDtoItem = itemMap.get("bicc");
            if (Objects.nonNull(biccPerfDtoItem)) {
                biccPerformance.setManHour(biccPerfDtoItem.getManHour());

                if (Objects.nonNull(biccPerfDtoItem.getIncomeHour()) && biccPerfDtoItem.getIncomeHour() > 0) {
                    DayPerformanceItem incomeHour = DayPerformanceItem.builder().userName("毕翠翠").effectDate(nowDateString).userOaAccount("bicc").ledgerType("shouyi_pingzheng").postName("股衍运营岗").manHour(biccPerfDtoItem.getIncomeHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(incomeHour);
                }

                if (Objects.nonNull(biccPerfDtoItem.getCustomerHour()) && biccPerfDtoItem.getCustomerHour() > 0) {
                    DayPerformanceItem customerHour = DayPerformanceItem.builder().userName("毕翠翠").effectDate(nowDateString).userOaAccount("bicc").ledgerType("kyc_org").postName("股衍运营岗").manHour(biccPerfDtoItem.getCustomerHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(customerHour);
                }

                if (Objects.nonNull(biccPerfDtoItem.getSwapHour()) && biccPerfDtoItem.getSwapHour() > 0) {
                    DayPerformanceItem swapHour = DayPerformanceItem.builder().userName("毕翠翠").effectDate(nowDateString).userOaAccount("bicc").ledgerType("jiaoyi_yewu").postName("股衍运营岗").manHour(biccPerfDtoItem.getSwapHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(swapHour);
                }

                if (Objects.nonNull(biccPerfDtoItem.getFoundationHour()) && biccPerfDtoItem.getFoundationHour() > 0) {
                    DayPerformanceItem foundationHour = DayPerformanceItem.builder().userName("毕翠翠").effectDate(nowDateString).userOaAccount("bicc").ledgerType("jijin_shenshu").postName("股衍运营岗").manHour(biccPerfDtoItem.getFoundationHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(foundationHour);
                }

                if (Objects.nonNull(biccPerfDtoItem.getSettlementHour()) && biccPerfDtoItem.getSettlementHour() > 0) {
                    DayPerformanceItem settlementHour = DayPerformanceItem.builder().userName("毕翠翠").effectDate(nowDateString).userOaAccount("bicc").ledgerType("qing_suan").postName("股衍运营岗").manHour(biccPerfDtoItem.getSettlementHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(settlementHour);
                }
            }
        }

        if (CollectionUtils.isNotEmpty(list)) {
            dayPerformanceRepository.saveAll(list);
        }

        if (CollectionUtils.isNotEmpty(itemList)) {
            dayPerformanceItemRepository.saveAll(itemList);
        }
    }



    @XxlJob("one_ZongHe")
    public void oneZongHe(){
        LocalDate start = LocalDate.of(2024, 8, 1);
        LocalDate end = LocalDate.of(2024, 11, 28);
        List<LocalDate> days = getDaysInRange(start, end);
        for (LocalDate day : days) {
            //查询今天是否涉及交易日
            Boolean isTradeDay = tradingDayService.getToday(day.toString().replaceAll("-",""));
            if (!isTradeDay) {
                continue;
            }
            zongHe(day.toString());
        }
    }

    /**
     * 创建当日综合运营岗绩效数据
     */
    @XxlJob("performance_zongHe")
    public void createZongHePerformance() {

        //查询今天是否涉及交易日
        Boolean isTradeDay = tradingDayService.getToday(DateUtils.getCurrentDate(formatDate));
        if (!isTradeDay) {
            return;
        }

        zongHe(DateUtils.getNowDateString());

    }

    @Transactional
    public void zongHe(String nowDateString){
        PlatformResponse<LedgerZongHeDto> response = desktopService.screenZongHe(nowDateString, null);
        if (Objects.isNull(response)) {
            return;
        }
        LedgerZongHeDto data = response.getData();
        if (Objects.isNull(data)) {
            return;
        }

        List<DayPerformance> list = Lists.newArrayList();
        List<DayPerformanceItem> itemList = Lists.newArrayList();

//        perfMap.put("肖丹", LedgerZongHeDtoItem.builder().userName("肖丹").postName("综合运营岗").userOaAccount("xiaod").dangWuHour(0).zongHeHour(0).renLiHour(0).xiTongHour(0).ziYouZiJinHour(0).jiaoYiDanYuanHour(0).qiTaHour(0).manHour(0).build());
//        perfMap.put("周洁", LedgerZongHeDtoItem.builder().userName("周洁").postName("综合运营岗").userOaAccount("zhoujie").dangWuHour(0).zongHeHour(0).renLiHour(0).xiTongHour(0).ziYouZiJinHour(0).jiaoYiDanYuanHour(0).qiTaHour(0).manHour(0).build());

        DayPerformance xiaod = DayPerformance.builder().userName("肖丹").userOaAccount("xiaod").postName("综合运营岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance zhoujiePer = DayPerformance.builder().userName("周洁").userOaAccount("zhoujie").postName("综合运营岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        list.add(xiaod);
        list.add(zhoujiePer);
//        xiaodDto.setDangWuHour(dayPerformanceItemService.findUserSumManHour("xiaod", startTime, endTime, "dangwu"));
//        xiaodDto.setZongHeHour(dayPerformanceItemService.findUserSumManHour("xiaod", startTime, endTime, "zonghe_shiwu"));
//        xiaodDto.setRenLiHour(dayPerformanceItemService.findUserSumManHour("xiaod", startTime, endTime, "renli_shixiang"));
//        xiaodDto.setXiTongHour(dayPerformanceItemService.findUserSumManHour("xiaod", startTime, endTime, "xitong_quanxian_gl"));
//        xiaodDto.setZiYouZiJinHour(dayPerformanceItemService.findUserSumManHour("xiaod", startTime, endTime, "zhanghu_guanli"));
//        xiaodDto.setJiaoYiDanYuanHour(dayPerformanceItemService.findUserSumManHour("xiaod", startTime, endTime, "jiaoyi_danyuan_gl"));
//        xiaodDto.setQiTaHour(dayPerformanceItemService.findUserSumManHour("xiaod", startTime, endTime, "jrscqt"));
        List<LedgerZongHeDtoItem> perfList = data.getPerfList();
        if (CollectionUtils.isNotEmpty(perfList)) {
            Map<String, LedgerZongHeDtoItem> itemMap = perfList.stream().collect(Collectors.toMap(LedgerZongHeDtoItem::getUserOaAccount, e -> e, (i, j) -> j));

            LedgerZongHeDtoItem xiaodPerfDtoItem = itemMap.get("xiaod");
            if (Objects.nonNull(xiaodPerfDtoItem)) {
                xiaod.setManHour(xiaodPerfDtoItem.getManHour());

                if (Objects.nonNull(xiaodPerfDtoItem.getDangWuHour()) && xiaodPerfDtoItem.getDangWuHour() > 0) {
                    DayPerformanceItem dangwu = DayPerformanceItem.builder().userName("肖丹").effectDate(nowDateString).userOaAccount("xiaod").ledgerType("dangwu").postName("综合运营岗").manHour(xiaodPerfDtoItem.getDangWuHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(dangwu);
                }

                if (Objects.nonNull(xiaodPerfDtoItem.getZongHeHour()) && xiaodPerfDtoItem.getZongHeHour() > 0) {
                    DayPerformanceItem zongHeShiWu = DayPerformanceItem.builder().userName("肖丹").effectDate(nowDateString).userOaAccount("xiaod").ledgerType("zonghe_shiwu").postName("综合运营岗").manHour(xiaodPerfDtoItem.getZongHeHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(zongHeShiWu);
                }

                if (Objects.nonNull(xiaodPerfDtoItem.getRenLiHour()) && xiaodPerfDtoItem.getRenLiHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("肖丹").effectDate(nowDateString).userOaAccount("xiaod").ledgerType("renli_shixiang").postName("综合运营岗").manHour(xiaodPerfDtoItem.getRenLiHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }

                if (Objects.nonNull(xiaodPerfDtoItem.getXiTongHour()) && xiaodPerfDtoItem.getXiTongHour() > 0) {
                    DayPerformanceItem xiTong = DayPerformanceItem.builder().userName("肖丹").effectDate(nowDateString).userOaAccount("xiaod").ledgerType("xitong_quanxian_gl").postName("综合运营岗").manHour(xiaodPerfDtoItem.getXiTongHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(xiTong);
                }

                if (Objects.nonNull(xiaodPerfDtoItem.getZiYouZiJinHour()) && xiaodPerfDtoItem.getZiYouZiJinHour() > 0) {
                    DayPerformanceItem zhangHu = DayPerformanceItem.builder().userName("肖丹").effectDate(nowDateString).userOaAccount("xiaod").ledgerType("zhanghu_guanli").postName("综合运营岗").manHour(xiaodPerfDtoItem.getZiYouZiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(zhangHu);
                }

                if (Objects.nonNull(xiaodPerfDtoItem.getJiaoYiDanYuanHour()) && xiaodPerfDtoItem.getJiaoYiDanYuanHour() > 0) {
                    DayPerformanceItem jiaoYiDanYuan = DayPerformanceItem.builder().userName("肖丹").effectDate(nowDateString).userOaAccount("xiaod").ledgerType("jiaoyi_danyuan_gl").postName("综合运营岗").manHour(xiaodPerfDtoItem.getJiaoYiDanYuanHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(jiaoYiDanYuan);
                }

                if (Objects.nonNull(xiaodPerfDtoItem.getQiTaHour()) && xiaodPerfDtoItem.getQiTaHour() > 0) {
                    DayPerformanceItem qiTa = DayPerformanceItem.builder().userName("肖丹").effectDate(nowDateString).userOaAccount("xiaod").ledgerType("jrscqt").postName("综合运营岗").manHour(xiaodPerfDtoItem.getQiTaHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(qiTa);
                }


            }


            LedgerZongHeDtoItem zhoujie = itemMap.get("zhoujie");
            if (Objects.nonNull(zhoujie)) {
                zhoujiePer.setManHour(zhoujie.getManHour());

                if (Objects.nonNull(zhoujie.getDangWuHour()) && zhoujie.getDangWuHour() > 0) {
                    DayPerformanceItem dangwu = DayPerformanceItem.builder().userName("周洁").effectDate(nowDateString).userOaAccount("zhoujie").ledgerType("dangwu").postName("综合运营岗").manHour(zhoujie.getDangWuHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(dangwu);
                }

                if (Objects.nonNull(zhoujie.getZongHeHour()) && zhoujie.getZongHeHour() > 0) {
                    DayPerformanceItem zongHeShiWu = DayPerformanceItem.builder().userName("周洁").effectDate(nowDateString).userOaAccount("zhoujie").ledgerType("zonghe_shiwu").postName("综合运营岗").manHour(zhoujie.getZongHeHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(zongHeShiWu);
                }

                if (Objects.nonNull(zhoujie.getRenLiHour()) && zhoujie.getRenLiHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("周洁").effectDate(nowDateString).userOaAccount("zhoujie").ledgerType("renli_shixiang").postName("综合运营岗").manHour(zhoujie.getRenLiHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }

                if (Objects.nonNull(zhoujie.getXiTongHour()) && zhoujie.getXiTongHour() > 0) {
                    DayPerformanceItem xiTong = DayPerformanceItem.builder().userName("周洁").effectDate(nowDateString).userOaAccount("zhoujie").ledgerType("xitong_quanxian_gl").postName("综合运营岗").manHour(zhoujie.getXiTongHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(xiTong);
                }

                if (Objects.nonNull(zhoujie.getZiYouZiJinHour()) && zhoujie.getZiYouZiJinHour() > 0) {
                    DayPerformanceItem zhangHu = DayPerformanceItem.builder().userName("周洁").effectDate(nowDateString).userOaAccount("zhoujie").ledgerType("zhanghu_guanli").postName("综合运营岗").manHour(zhoujie.getZiYouZiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(zhangHu);
                }

                if (Objects.nonNull(zhoujie.getJiaoYiDanYuanHour()) && zhoujie.getJiaoYiDanYuanHour() > 0) {
                    DayPerformanceItem jiaoYiDanYuan = DayPerformanceItem.builder().userName("周洁").effectDate(nowDateString).userOaAccount("zhoujie").ledgerType("jiaoyi_danyuan_gl").postName("综合运营岗").manHour(zhoujie.getJiaoYiDanYuanHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(jiaoYiDanYuan);
                }

                if (Objects.nonNull(zhoujie.getQiTaHour()) && zhoujie.getQiTaHour() > 0) {
                    DayPerformanceItem qiTa = DayPerformanceItem.builder().userName("周洁").effectDate(nowDateString).userOaAccount("zhoujie").ledgerType("jrscqt").postName("综合运营岗").manHour(zhoujie.getQiTaHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(qiTa);
                }


            }


        }

        if (CollectionUtils.isNotEmpty(list)) {
            dayPerformanceRepository.saveAll(list);
        }

        if (CollectionUtils.isNotEmpty(itemList)) {
            dayPerformanceItemRepository.saveAll(itemList);
        }
    }



    @XxlJob("one_jiaoyi")
    public void oneJiaoYi(){
        LocalDate start = LocalDate.of(2024, 8, 1);
        LocalDate end = LocalDate.of(2024, 11, 28);
        List<LocalDate> days = getDaysInRange(start, end);
        for (LocalDate day : days) {
            //查询今天是否涉及交易日
            Boolean isTradeDay = tradingDayService.getToday(day.toString().replaceAll("-",""));
            if (!isTradeDay) {
                continue;
            }
            jiaoYi(day.toString());
        }
    }

    /**
     * 创建当日交易岗绩效数据
     */
    @XxlJob("performance_jiaoYi")
    public void createJiaoYiPerformance() {

        //查询今天是否涉及交易日
        Boolean isTradeDay = tradingDayService.getToday(DateUtils.getCurrentDate(formatDate));
        if (!isTradeDay) {
            return;
        }
        jiaoYi(DateUtils.getNowDateString());

    }

    @Transactional
    public void jiaoYi(String nowDateString){
        PlatformResponse<LedgerJiaoYiDto> response = desktopService.screenJiaoYi(nowDateString, null);
        if (Objects.isNull(response)) {
            return;
        }
        LedgerJiaoYiDto data = response.getData();
        if (Objects.isNull(data)) {
            return;
        }

        List<DayPerformance> list = Lists.newArrayList();
        List<DayPerformanceItem> itemList = Lists.newArrayList();

//        perfMap.put("范漪漪", LedgerJiaoYiDtoItem.builder().userName("范漪漪").userOaAccount("fanyy").postName("交易岗").daZongHour(0).ziYouGuZhiHour(0).jiJinHour(0).manHour(0).build());
//        perfMap.put("安恬", LedgerJiaoYiDtoItem.builder().userName("安恬").userOaAccount("ant").postName("交易岗").daZongHour(0).ziYouGuZhiHour(0).jiJinHour(0).manHour(0).build());
//        perfMap.put("陈诗佳", LedgerJiaoYiDtoItem.builder().userName("陈诗佳").userOaAccount("chenshj").postName("交易岗").daZongHour(0).ziYouGuZhiHour(0).jiJinHour(0).manHour(0).build());

        DayPerformance fanyyPer = DayPerformance.builder().userName("范漪漪").effectDate(nowDateString).userOaAccount("fanyy").postName("交易岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance antPer = DayPerformance.builder().userName("安恬").effectDate(nowDateString).userOaAccount("ant").postName("交易岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance chenshjPer = DayPerformance.builder().userName("陈诗佳").effectDate(nowDateString).userOaAccount("chenshj").postName("交易岗").manHour(0).effectDate(nowDateString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        list.add(fanyyPer);
        list.add(antPer);
        list.add(chenshjPer);

//               List<String> typeList = Arrays.asList("dazong_jiaoyi", "guzhi_xingquan", "jijin_tougu");

        List<LedgerJiaoYiDtoItem> perfList = data.getPerfList();
        if (CollectionUtils.isNotEmpty(perfList)) {
            Map<String, LedgerJiaoYiDtoItem> itemMap = perfList.stream().collect(Collectors.toMap(LedgerJiaoYiDtoItem::getUserOaAccount, e -> e, (i, j) -> j));


            LedgerJiaoYiDtoItem fanyyPerfDtoItem = itemMap.get("fanyy");
            if (Objects.nonNull(fanyyPerfDtoItem)) {
                fanyyPer.setManHour(fanyyPerfDtoItem.getManHour());

                if (Objects.nonNull(fanyyPerfDtoItem.getDaZongHour()) && fanyyPerfDtoItem.getDaZongHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("范漪漪").effectDate(nowDateString).userOaAccount("fanyy").ledgerType("dazong_jiaoyi").postName("交易岗").manHour(fanyyPerfDtoItem.getDaZongHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(fanyyPerfDtoItem.getZiYouGuZhiHour()) && fanyyPerfDtoItem.getZiYouGuZhiHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("范漪漪").effectDate(nowDateString).userOaAccount("fanyy").ledgerType("guzhi_xingquan").postName("交易岗").manHour(fanyyPerfDtoItem.getZiYouGuZhiHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(fanyyPerfDtoItem.getJiJinHour()) && fanyyPerfDtoItem.getJiJinHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("范漪漪").effectDate(nowDateString).userOaAccount("fanyy").ledgerType("jijin_tougu").postName("交易岗").manHour(fanyyPerfDtoItem.getJiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
            }

            LedgerJiaoYiDtoItem antPerfDtoItem = itemMap.get("ant");
            if (Objects.nonNull(antPerfDtoItem)) {
                antPer.setManHour(antPerfDtoItem.getManHour());

                if (Objects.nonNull(antPerfDtoItem.getDaZongHour()) && antPerfDtoItem.getDaZongHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("安恬").effectDate(nowDateString).userOaAccount("ant").ledgerType("dazong_jiaoyi").postName("交易岗").manHour(antPerfDtoItem.getDaZongHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(antPerfDtoItem.getZiYouGuZhiHour()) && antPerfDtoItem.getZiYouGuZhiHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("安恬").effectDate(nowDateString).userOaAccount("ant").ledgerType("guzhi_xingquan").postName("交易岗").manHour(antPerfDtoItem.getZiYouGuZhiHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(antPerfDtoItem.getJiJinHour()) && antPerfDtoItem.getJiJinHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("安恬").effectDate(nowDateString).userOaAccount("ant").ledgerType("jijin_tougu").postName("交易岗").manHour(antPerfDtoItem.getJiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
            }

            LedgerJiaoYiDtoItem chenshjPerfDtoItem = itemMap.get("chenshj");
            if (Objects.nonNull(chenshjPerfDtoItem)) {
                chenshjPer.setManHour(chenshjPerfDtoItem.getManHour());

                if (Objects.nonNull(chenshjPerfDtoItem.getDaZongHour()) && chenshjPerfDtoItem.getDaZongHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("陈诗佳").effectDate(nowDateString).userOaAccount("chenshj").ledgerType("dazong_jiaoyi").postName("交易岗").manHour(chenshjPerfDtoItem.getDaZongHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(chenshjPerfDtoItem.getZiYouGuZhiHour()) && chenshjPerfDtoItem.getZiYouGuZhiHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("陈诗佳").effectDate(nowDateString).userOaAccount("chenshj").ledgerType("guzhi_xingquan").postName("交易岗").manHour(chenshjPerfDtoItem.getZiYouGuZhiHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(chenshjPerfDtoItem.getJiJinHour()) && chenshjPerfDtoItem.getJiJinHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("陈诗佳").effectDate(nowDateString).userOaAccount("chenshj").ledgerType("jijin_tougu").postName("交易岗").manHour(chenshjPerfDtoItem.getJiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
            }

        }

        if (CollectionUtils.isNotEmpty(list)) {
            dayPerformanceRepository.saveAll(list);
        }

        if (CollectionUtils.isNotEmpty(itemList)) {
            dayPerformanceItemRepository.saveAll(itemList);
        }

    }



    @XxlJob("one_guShou")
    public void one_guShou(){
        LocalDate start = LocalDate.of(2024, 8, 1);
        LocalDate end = LocalDate.of(2024, 11, 28);
        List<LocalDate> days = getDaysInRange(start, end);
        for (LocalDate day : days) {
            //查询今天是否涉及交易日
            Boolean isTradeDay = tradingDayService.getToday(day.toString().replaceAll("-",""));
            if (!isTradeDay) {
                continue;
            }
            guShou(day.toString());
        }
    }

    /**
     * 创建当日固收岗绩效数据
     */
    @XxlJob("performance_guShow")
    public void createGuShouPerformance() {

        //查询今天是否涉及交易日
        Boolean isTradeDay = tradingDayService.getToday(DateUtils.getCurrentDate(formatDate));
        if (!isTradeDay) {
            return;
        }
        guShou(DateUtils.getNowDateString());


    }

    @Transactional
    public void guShou(String date){
        PlatformResponse<LedgerGuShouDto> response = desktopService.screenGuShou(date, null);
        if (Objects.isNull(response)) {
            return;
        }
        LedgerGuShouDto data = response.getData();
        if (Objects.isNull(data)) {
            return;
        }

        List<DayPerformance> list = Lists.newArrayList();
        List<DayPerformanceItem> itemList = Lists.newArrayList();

//        perfMap.put("安恬", LedgerGuShouDtoItem.builder().userName("安恬").userOaAccount("ant").postName("固收运营岗").baoBiaoHour(0).huHuanHour(0).jieSuanHour(0).ziJinHour(0).manHour(0).build());
//        perfMap.put("范漪漪", LedgerGuShouDtoItem.builder().userName("范漪漪").userOaAccount("fanyy").postName("固收运营岗").baoBiaoHour(0).huHuanHour(0).jieSuanHour(0).ziJinHour(0).manHour(0).build());
//        perfMap.put("何泉震", LedgerGuShouDtoItem.builder().userName("何泉震").userOaAccount("heqzh").postName("固收运营岗").baoBiaoHour(0).huHuanHour(0).jieSuanHour(0).ziJinHour(0).manHour(0).build());
//        perfMap.put("姚金岐", LedgerGuShouDtoItem.builder().userName("姚金岐").userOaAccount("yaojq").postName("固收运营岗").baoBiaoHour(0).huHuanHour(0).jieSuanHour(0).ziJinHour(0).manHour(0).build());

        DayPerformance fanyyPer = DayPerformance.builder().userName("范漪漪").userOaAccount("fanyy").postName("固收运营岗").manHour(0).effectDate(date).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance antPer = DayPerformance.builder().userName("安恬").userOaAccount("ant").postName("固收运营岗").manHour(0).effectDate(date).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance heqzhPer = DayPerformance.builder().userName("何泉震").userOaAccount("heqzh").postName("固收运营岗").manHour(0).effectDate(date).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance yaojqPer = DayPerformance.builder().userName("姚金岐").userOaAccount("yaojq").postName("固收运营岗").manHour(0).effectDate(date).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        list.add(fanyyPer);
        list.add(antPer);
        list.add(heqzhPer);
        list.add(yaojqPer);

//        List<String> typeList = Arrays.asList("gushou_yunying_bb", "gushou_yunying_hh", "gushou_yunying_js", "gushou_yunying_zj");

        List<LedgerGuShouDtoItem> perfList = data.getPerfList();
        if (CollectionUtils.isNotEmpty(perfList)) {
            Map<String, LedgerGuShouDtoItem> itemMap = perfList.stream().collect(Collectors.toMap(LedgerGuShouDtoItem::getUserOaAccount, e -> e, (i, j) -> j));


            LedgerGuShouDtoItem fanyyPerfDtoItem = itemMap.get("fanyy");
            if (Objects.nonNull(fanyyPerfDtoItem)) {
                fanyyPer.setManHour(fanyyPerfDtoItem.getManHour());

                if (Objects.nonNull(fanyyPerfDtoItem.getBaoBiaoHour()) && fanyyPerfDtoItem.getBaoBiaoHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("范漪漪").effectDate(date).userOaAccount("fanyy").ledgerType("gushou_yunying_bb").postName("固收运营岗").manHour(fanyyPerfDtoItem.getBaoBiaoHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(fanyyPerfDtoItem.getHuHuanHour()) && fanyyPerfDtoItem.getHuHuanHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("范漪漪").effectDate(date).userOaAccount("fanyy").ledgerType("gushou_yunying_hh").postName("固收运营岗").manHour(fanyyPerfDtoItem.getHuHuanHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(fanyyPerfDtoItem.getJieSuanHour()) && fanyyPerfDtoItem.getJieSuanHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("范漪漪").effectDate(date).userOaAccount("fanyy").ledgerType("gushou_yunying_js").postName("固收运营岗").manHour(fanyyPerfDtoItem.getJieSuanHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
                if (Objects.nonNull(fanyyPerfDtoItem.getZiJinHour()) && fanyyPerfDtoItem.getZiJinHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("范漪漪").effectDate(date).userOaAccount("fanyy").ledgerType("gushou_yunying_zj").postName("固收运营岗").manHour(fanyyPerfDtoItem.getZiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
            }

            LedgerGuShouDtoItem antPerfDtoItem = itemMap.get("ant");
            if (Objects.nonNull(antPerfDtoItem)) {
                antPer.setManHour(antPerfDtoItem.getManHour());

                if (Objects.nonNull(antPerfDtoItem.getBaoBiaoHour()) && antPerfDtoItem.getBaoBiaoHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("安恬").effectDate(date).userOaAccount("ant").ledgerType("gushou_yunying_bb").postName("固收运营岗").manHour(antPerfDtoItem.getBaoBiaoHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(antPerfDtoItem.getHuHuanHour()) && antPerfDtoItem.getHuHuanHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("安恬").effectDate(date).userOaAccount("ant").ledgerType("gushou_yunying_hh").postName("固收运营岗").manHour(antPerfDtoItem.getHuHuanHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(antPerfDtoItem.getJieSuanHour()) && antPerfDtoItem.getJieSuanHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("安恬").effectDate(date).userOaAccount("ant").ledgerType("gushou_yunying_js").postName("固收运营岗").manHour(antPerfDtoItem.getJieSuanHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
                if (Objects.nonNull(antPerfDtoItem.getZiJinHour()) && antPerfDtoItem.getZiJinHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("安恬").effectDate(date).userOaAccount("ant").ledgerType("gushou_yunying_zj").postName("固收运营岗").manHour(antPerfDtoItem.getZiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
            }

            LedgerGuShouDtoItem heqzhPerfDtoItem = itemMap.get("heqzh");
            if (Objects.nonNull(heqzhPerfDtoItem)) {
                heqzhPer.setManHour(heqzhPerfDtoItem.getManHour());

                if (Objects.nonNull(heqzhPerfDtoItem.getBaoBiaoHour()) && heqzhPerfDtoItem.getBaoBiaoHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("何泉震").effectDate(date).userOaAccount("heqzh").ledgerType("gushou_yunying_bb").postName("固收运营岗").manHour(heqzhPerfDtoItem.getBaoBiaoHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(heqzhPerfDtoItem.getHuHuanHour()) && heqzhPerfDtoItem.getHuHuanHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("何泉震").effectDate(date).userOaAccount("heqzh").ledgerType("gushou_yunying_hh").postName("固收运营岗").manHour(heqzhPerfDtoItem.getHuHuanHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(heqzhPerfDtoItem.getJieSuanHour()) && heqzhPerfDtoItem.getJieSuanHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("何泉震").effectDate(date).userOaAccount("heqzh").ledgerType("gushou_yunying_js").postName("固收运营岗").manHour(heqzhPerfDtoItem.getJieSuanHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
                if (Objects.nonNull(heqzhPerfDtoItem.getZiJinHour()) && heqzhPerfDtoItem.getZiJinHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("何泉震").effectDate(date).userOaAccount("heqzh").ledgerType("gushou_yunying_zj").postName("固收运营岗").manHour(heqzhPerfDtoItem.getZiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
            }

            LedgerGuShouDtoItem yaojqPerfDtoItem = itemMap.get("yaojq");
            if (Objects.nonNull(yaojqPerfDtoItem)) {
                yaojqPer.setManHour(yaojqPerfDtoItem.getManHour());

                if (Objects.nonNull(yaojqPerfDtoItem.getBaoBiaoHour()) && yaojqPerfDtoItem.getBaoBiaoHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("姚金岐").effectDate(date).userOaAccount("yaojq").ledgerType("gushou_yunying_bb").postName("固收运营岗").manHour(yaojqPerfDtoItem.getBaoBiaoHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(yaojqPerfDtoItem.getHuHuanHour()) && yaojqPerfDtoItem.getHuHuanHour() > 0) {
                    itemList.add(DayPerformanceItem.builder().userName("姚金岐").effectDate(date).userOaAccount("yaojq").ledgerType("gushou_yunying_hh").postName("固收运营岗").manHour(yaojqPerfDtoItem.getHuHuanHour()).createTime(LocalDateTime.now()).build());
                }

                if (Objects.nonNull(yaojqPerfDtoItem.getJieSuanHour()) && yaojqPerfDtoItem.getJieSuanHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("姚金岐").effectDate(date).userOaAccount("yaojq").ledgerType("gushou_yunying_js").postName("固收运营岗").manHour(yaojqPerfDtoItem.getJieSuanHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
                if (Objects.nonNull(yaojqPerfDtoItem.getZiJinHour()) && yaojqPerfDtoItem.getZiJinHour() > 0) {
                    DayPerformanceItem renLi = DayPerformanceItem.builder().userName("姚金岐").effectDate(date).userOaAccount("yaojq").ledgerType("gushou_yunying_zj").postName("固收运营岗").manHour(yaojqPerfDtoItem.getZiJinHour()).createTime(LocalDateTime.now()).build();
                    itemList.add(renLi);
                }
            }

        }

        if (CollectionUtils.isNotEmpty(list)) {
            dayPerformanceRepository.saveAll(list);
        }

        if (CollectionUtils.isNotEmpty(itemList)) {
            dayPerformanceItemRepository.saveAll(itemList);
        }
    }

    /**
     * 创建当日清算岗绩效数据
     */
    @XxlJob("performance_qingSuqan")
    public void createQingSuanPerformance() {

        LocalDate today = LocalDate.now(); // 获取今天的日期
        LocalDate yesterday = today.minusDays(1); // 减去一天，得到前一天的日期
        // 创建一个DateTimeFormatter实例来定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter tradeFormatter = DateTimeFormatter.ofPattern(formatDate);

        String yesterdayString = yesterday.format(formatter);

        //查询今天是否涉及交易日
        Boolean isTradeDay = tradingDayService.getToday(yesterday.format(tradeFormatter));
        if (!isTradeDay) {
            return;
        }

        PlatformResponse<LedgerQingSuanDto> response = desktopService.screenQingSuan(yesterdayString, null);
        if (Objects.isNull(response)) {
            return;
        }
        LedgerQingSuanDto data = response.getData();
        if (Objects.isNull(data)) {
            return;
        }

        List<DayPerformance> list = Lists.newArrayList();

//        perfMap.put("杨珂", LedgerQingSuanDtoItem.builder().userName("杨珂").userOaAccount("yangke").postName("清算岗").beiDongHour(0).zhuDongHour(0).manHour(0).build());
//        perfMap.put("徐苏", LedgerQingSuanDtoItem.builder().userName("徐苏").userOaAccount("xus").postName("清算岗").beiDongHour(0).zhuDongHour(0).manHour(0).build());
//        perfMap.put("刘涵煜", LedgerQingSuanDtoItem.builder().userName("刘涵煜").userOaAccount("liuhy").postName("清算岗").beiDongHour(0).zhuDongHour(0).manHour(0).build());
//        perfMap.put("许晟晖", LedgerQingSuanDtoItem.builder().userName("许晟晖").userOaAccount("xush").postName("清算岗").beiDongHour(0).zhuDongHour(0).manHour(0).build());
//        perfMap.put("徐浩昕", LedgerQingSuanDtoItem.builder().userName("徐浩昕").userOaAccount("xhx").postName("清算岗").beiDongHour(0).zhuDongHour(0).manHour(0).build());

        DayPerformance yangkePer = DayPerformance.builder().userName("杨珂").userOaAccount("yangke").postName("清算岗").manHour(0).effectDate(yesterdayString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance xusPer = DayPerformance.builder().userName("徐苏").userOaAccount("xus").postName("清算岗").manHour(0).effectDate(yesterdayString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance liuhyPer = DayPerformance.builder().userName("刘涵煜").userOaAccount("liuhy").postName("清算岗").manHour(0).effectDate(yesterdayString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance xushPer = DayPerformance.builder().userName("许晟晖").userOaAccount("xush").postName("清算岗").manHour(0).effectDate(yesterdayString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();
        DayPerformance xhxPer = DayPerformance.builder().userName("徐浩昕").userOaAccount("xhx").postName("清算岗").manHour(0).effectDate(yesterdayString).accordHour(0).passiveHour(0).createTime(LocalDateTime.now()).build();

        list.add(yangkePer);
        list.add(xusPer);
        list.add(liuhyPer);
        list.add(xushPer);
        list.add(xhxPer);

        List<LedgerQingSuanDtoItem> perfList = data.getPerfList();
        if (CollectionUtils.isNotEmpty(perfList)) {
            Map<String, LedgerQingSuanDtoItem> itemMap = perfList.stream().collect(Collectors.toMap(LedgerQingSuanDtoItem::getUserOaAccount, e -> e, (i, j) -> j));

            LedgerQingSuanDtoItem yangkePerfDtoItem = itemMap.get("yangke");
            if (Objects.nonNull(yangkePerfDtoItem)) {
                yangkePer.setAccordHour(yangkePerfDtoItem.getManHour());
                yangkePer.setManHour(yangkePerfDtoItem.getManHour());
            }

            LedgerQingSuanDtoItem xusPerfDtoItem = itemMap.get("xus");
            if (Objects.nonNull(xusPerfDtoItem)) {
                xusPer.setAccordHour(xusPerfDtoItem.getManHour());
                xusPer.setManHour(xusPerfDtoItem.getManHour());
            }

            LedgerQingSuanDtoItem liuhyPerfDtoItem = itemMap.get("liuhy");
            if (Objects.nonNull(liuhyPerfDtoItem)) {
                liuhyPer.setAccordHour(liuhyPerfDtoItem.getManHour());
                liuhyPer.setManHour(liuhyPerfDtoItem.getManHour());
            }

            LedgerQingSuanDtoItem xushPerfDtoItem = itemMap.get("xush");
            if (Objects.nonNull(xushPerfDtoItem)) {
                xushPer.setAccordHour(xushPerfDtoItem.getManHour());
                xushPer.setManHour(xushPerfDtoItem.getManHour());
            }

            LedgerQingSuanDtoItem xhxPerfDtoItem = itemMap.get("xhx");
            if (Objects.nonNull(xhxPerfDtoItem)) {
                xhxPer.setAccordHour(xhxPerfDtoItem.getManHour());
                xhxPer.setManHour(xhxPerfDtoItem.getManHour());
            }

        }

        if (CollectionUtils.isNotEmpty(list)) {
            dayPerformanceRepository.saveAll(list);
        }

    }


}
